<template>
  <div class="container">
    <el-button
      @click="
        () => {
          isEdit = false;
          dialogVisible = true;
          form.type = '1';
        }
      "
      >新增菜品分类</el-button
    >
    <el-button
      type="primary"
      @click="
        () => {
          isEdit = false;
          dialogVisible = true;
          form.type = '2';
        }
      "
      >新增套餐分类</el-button
    >
    <el-table :data="tableData" style="width: 100%" :border="true">
      <el-table-column prop="name" label="分类名称"> </el-table-column>
      <el-table-column label="分类类型">
        <template v-slot="{ row }">
          {{ Number(row.type) === 1 ? "菜品" : "套餐" }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="操作时间"> </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link
            type="primary"
            @click="
              () => {
                isEdit = true;
                dialogVisible = true;
                form = row;
              }
            "
            >修改</el-link
          >
          <el-divider direction="vertical"></el-divider>
          <el-link type="danger">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="`${isEdit ? '修改' : '新增'}${
        Number(form.type) === 1 ? '菜品' : '套餐'
      }分类`"
      :visible.sync="dialogVisible"
      width="30%"
      @close="handleCancel"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCategories, addCategory, updateCategory } from "@/api/category";
export default {
  data() {
    return {
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      tableData: [],
      dialogVisible: false,
      form: {
        name: "",
        type: "1",
        sort: "",
      },
      isEdit: false,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getCategories(this.pageInfo).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    handleSave() {
      if (this.isEdit) {
        updateCategory(this.form).then((res) => {
          this.$message.success("修改成功");
          this.dialogVisible = false;
          this.form = {};
          this.init();
        });
      } else {
        addCategory(this.form).then((res) => {
          this.$message.success("新增成功");
          this.dialogVisible = false;
          this.form = {};
          this.init();
        });
      }
    },
    handleCancel() {
      this.dialogVisible = false;
      this.form = {};
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 35px;
  .el-table {
    margin-top: 22px;
  }
  .footer {
    margin-top: 22px;
    display: flex;
    justify-content: center;
  }
}
</style>
